import { IBizLabTemplate } from '@ibizlab/template';

/**
 * 自定义参数助手
 *
 * @export
 * @class CustomParams
 */
export class CustomParams {
    /**
     * Creates an instance of CustomParams.
     * @param {IBizLabTemplate} template
     * @memberof CustomParams
     */
    constructor(template: IBizLabTemplate) {
        template.engine.registerHelper('CustomParams', (...args: any[]) => {
            const type = args[0];
			const params = args[1] as any;
            switch (type) {
                case 'ctrl':
                    return this.getCtrlParams(params);
                case 'editor':
                    return this.getEditorParams(params);
                default:
                    return '';
            }
        })
    }

    /**
     * 获取部件参数
     *
     * @param {*} ctrl
     * @memberof CustomParams
     */
    public getCtrlParams(ctrl: any): string {
        let ctrlParams: string = '{}';
        const customParams: string = this.getCustomParams(ctrl);
        if (customParams) {
            ctrlParams = `{ ${customParams} }`;
        }
        return ctrlParams
    }

    /**
     * 获取编辑器参数
     *
     * @param {*} params
     * @memberof CustomParams
     */
    public getEditorParams(editor: any): string {
        let editParams: string = `{ editorType: '${editor.editorType}', ${editor.editorStyle ? "editorStyle: '" + editor.editorStyle + "' " : ""}}`;
        const customParams: string = this.getCustomParams(editor.editorParams);
        if (customParams) {
            editParams = `{ editorType: '${editor.editorType}', ${editor.editorStyle ? "editorStyle: '" + editor.editorStyle + "', " : ""}${customParams} }`; ;
        }
        return editParams
    }

    /**
     * 获取自定义参数
     *
     * @param {*} params 
     * @return {*}  {string}
     * @memberof CustomParams
     */
    public getCustomParams(params: any): string {
        let customParams: string = '';
        if (params && Object.keys(params).length > 0) {
            const length: number = Object.keys(params).length;
            Object.keys(params).forEach((name: string, index: number) => {
                const param: string = params[name];
                if (param && !name.startsWith('SRFNAVPARAM.') && !name.startsWith('SRFNAVCTX.')) {
                    if (param.startsWith('{') && param.endsWith('}')) {
                        // 处理对象类型
                        customParams += `${name}: ${param}${index < length - 1 ? ', ' : ''}`
                    } else {
                        try {
                            // 处理标准类型(number, boolean, array, null, undefined)
                            eval(param);
                            customParams += `${name}: ${param}${index < length - 1 ? ', ' : ''}`
                        } catch (error) {
                            // 处理事件 以on开头的
                            if (name.startsWith('on') && name.length > 2) {
                                customParams += `${name}: ${param}${index < length - 1 ? ', ' : ''}`
                            } else {
                                customParams += `${name}: '${param}'${index < length - 1 ? ', ' : ''}`
                            }
                        }
                    }
                }
            })
        }
        return customParams
    }
}